package com.example.lanqiaodemo1.java8plus;

import java.io.*;
import java.util.*;


@SuppressWarnings({"all"})
public class JavaTest8 {

    static ArrayList<Student> studentsList = new ArrayList<>();

    static Scanner scanner = new Scanner(System.in);


    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        init();
        beginProject();
    }



    public static void beginProject() throws IOException, InterruptedException {
        String chose;
        do {
            showMenu();
            System.out.println("请输入你的选择");
            chose = scanner.next();
            switch (chose) {
                case "1":
                    // todo 1 录入用户信息
                    getUserInformation();
                    break;
                case "2":
                    // todo 查看所有的用户信息
                    showStudentInformation();
                    //这个可以使我们当前的程序进行一个睡眠处理 实现的就是通过进程来控制我们的睡眠时间
                    Thread.currentThread().sleep(1000);
                    break;
                case "3":
                    // todo 按照学号升序排列 getStudentInformationByIdAsc
                    getStudentInformationByIdDesc();
                    //进行的时间的休眠 也就是
                    Thread.currentThread().sleep(1000);
//                    System.out.println("3");
                    break;
                case "4":
                    //todo 按照学号查询用户的信息  getStudentInfoById
                    getStudentInfoById();
                    Thread.currentThread().sleep(1000);

                    System.out.println("4");
                    break;
                case "5":
                    // todo 按照姓名查询用户的信息  getStudentInfoByName
                    getStudentInfoByName();
                    Thread.currentThread().sleep(1000);

                    System.out.println("5");
                    break;
                case "6":
                    // todo 获取每一个用户的 总分和 getAllStudentSumScoreAndAverageScore
                    getAllStudentSumScoreAndAverageScore();
                    Thread.currentThread().sleep(1000);
                    // System.out.println("6");
                    break;
                case "7":
                    // todo 按照用户 平均分 降序排列用户信息
                    getStudentInfoByAverageDesc();
                    Thread.currentThread().sleep(1000);

                    //System.out.println("7");
                    break;
                case "9":
                    // todo save 防止不正常的退出
                    break;

            }

        } while (!chose.equals("exit"));
//        todo 在这里进行将我们的文件进行保存 因为这个时候我们的整个程序是结束的 所以说 我们可以放心的写在这里
        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("D:\\students.txt"));
        outputStream.writeObject(studentsList);

    }

    //获取所有的用户的分数的和 已经他们的平均成绩
    public static void getAllStudentSumScoreAndAverageScore() {
        for (Student student : studentsList) {
            int sum = 0;
            int average = 0;
            for (int i = 0; i < student.getStudentScore().size(); i++) {
                sum += student.studentScore.get(i).getClassScore();
            }
            System.out.println(student.getName() + "的总分是" + sum + "  " + student.getName() + "的平均分是" + sum / student.getStudentScore().size());
        }
    }

    //按照用户的平均分进行降序排列
    public static void getStudentInfoByAverageDesc() {
        //获取用户的平均分 从大到小进行排序
        Collections.sort(studentsList, (o1, o2) -> (int) (o2.getAverageScore() - o1.getAverageScore()));
        for (Student student : studentsList) {
            System.out.println(student);
        }
    }

    //将学生的信息按照id进行降序排列
    public static void getStudentInformationByIdDesc() {
        //这样的效果是升序 最小的在前面
        Collections.sort(studentsList, (o1, o2) -> (int) (o2.getId() - o1.getId()));
        for (Student student : studentsList) {
            System.out.println(student);
        }
    }

    //获取用户的信息通过id进行查询
    public static void getStudentInfoById() {
        // todo 查询操作
        System.out.println("请输入你要查询的id");
        Scanner scanner = new Scanner(System.in);
        long id = scanner.nextLong();

        for (Student student : studentsList) {
            if (id == student.getId()) {
                System.out.println(student);
                return;
            }
        }
        System.out.println("请检查学号 查无此人");
    }


    //    获取用户信息通过name
    public static void getStudentInfoByName() {
        System.out.println("请输入用户的姓名");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        for (Student student : studentsList) {
            if (student.getName().equals(name)) {
                System.out.println(student);
                return;
            }
        }
        System.out.println("查无此人 请检查输入的用户姓名");
    }


    public static void init() throws IOException, ClassNotFoundException {
        //每一次进去我们的主程序 我们要确保我们的数据都是最新的数据  只有这样的话 我们才能讲我们的数据全部真正的保存
        ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("D:\\students.txt"));
        try {
            //必须进行捕获一下 不然会出现错误的
            //读完了，可是抛出了一个EOFException，
            // 这是什么鬼？问老师后，老师说这是对象流读到文件末尾后抛出的一个异常，代表着文件读完了。
            // 好吧，看来这种读取是没问题的，有问题的是我的判断方式。老师说可以用这个异常作为读完的标志，就是读到出现这个异常，代表读完了。
            studentsList = (ArrayList<Student>) inputStream.readObject();
        } catch (EOFException e) {
            System.out.println("项目初始化完成");
        } catch (InvalidClassException e) {
            System.out.println();
        }
    }

    //录入用户信息
    public static void getUserInformation() {
        Student student = new Student();
        System.out.println("请输入学号");
        long id = scanner.nextLong();
        student.setId(id);
        System.out.println("请输入学生姓名");
        String name = scanner.next();
        student.setName(name);
        System.out.println("请输入学生班级");
        String classRoom = scanner.next();
        student.setClassRoom(classRoom);
        System.out.println("请输入学生的性别");
        String sex = scanner.next();
        student.setSex(sex);
        for (int j = 0; j < 2; j++) {
            System.out.println("请输入要录入的学生的学科:");
            String ClassName = scanner.next();
            System.out.println("请输入学生的成绩:");
            double studentScore = scanner.nextDouble();
            student.studentScore.add(new score(ClassName, studentScore));
        }
        studentsList.add(student);
        System.out.println("录入信息成功 请进行下一步操作");
    }


    //展示菜单
    public static void showMenu() {
        //实现菜单的功能
        System.out.println("1.添加学生信息");
        System.out.println("2.查看所有学生信息");
        System.out.println("3.查看学生信息 按照学号降序排序");
        System.out.println("4.根据学号查询学生信息");
        System.out.println("5.根据学生姓名查询学生的信息");
        System.out.println("6.获取每一个学生的总分和");
        System.out.println("7.按照学生的平均分降序排列集合中的数据");
        System.out.println("8.exit");
    }

    //展示学生的信息
    public static void showStudentInformation() {
        // 默认进行排序 这个是进行升序的排列
        Collections.sort(studentsList, (o1, o2) -> (int) (o1.getId() - o2.getId()));
        for (Student student : studentsList) {
            System.out.println(student);
        }
    }
}

class Student implements Serializable {
    private Long id;
    private String name;
    private String sex;
    private String classRoom;
    public List<score> studentScore;

    public Student(Long id, String name, String sex, String classRoom, List<score> studentScore) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.classRoom = classRoom;
        this.studentScore = studentScore;
    }

    public double getAverageScore() {
        double sum = 0;
        for (int i = 0; i < studentScore.size(); i++) {
            sum += studentScore.get(i).getClassScore();
        }
        return sum / (studentScore.size());

    }

    public Student() {
        studentScore = new ArrayList<>();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getClassRoom() {
        return classRoom;
    }

    public void setClassRoom(String classRoom) {
        this.classRoom = classRoom;
    }

    public List<score> getStudentScore() {
        return studentScore;
    }

    public void setStudentScore(List<score> studentScore) {
        this.studentScore = studentScore;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", classRoom='" + classRoom + '\'' +
                ", studentScore=" + studentScore +
                '}';
    }
}

class score implements Serializable {
    private String ClassName;
    private double ClassScore;

    public score(String className, double classScore) {
        ClassName = className;
        ClassScore = classScore;
    }

    @Override
    public String toString() {
        return "score{" +
                "ClassName='" + ClassName + '\'' +
                ", ClassScore=" + ClassScore +
                '}';
    }

    public String getClassName() {
        return ClassName;
    }

    public void setClassName(String className) {
        ClassName = className;
    }

    public double getClassScore() {
        return ClassScore;
    }

    public void setClassScore(double classScore) {
        ClassScore = classScore;
    }
}
